/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
 
using System.Text;
 
using Xtee.Core.Mime.Message;
using System.IO;
using NUnit.Framework;

namespace Xtee.Core.Test.Mime.DataTypes
{
    [TestFixture]
    public class Base64BodyInfoTests : TestBase
    {
        static string testdata = @"
  
		           Настоящая художественная ковка изделий. Протезирование и имплантация - вопросы стоматологу. Свежие деловые объявления из первых рук. 
					 Легкая тележка на склад. Simplex предлагает жалюзи и рулонные шторы. Рольставни для Москвы и области. ГУП (о) пластиковые окна ПВХ.
					 Профессиональное создание сайта для компаний.
					 Узкие металлические стеллажи архивные и для бумаг. Цветные пластиковые окна пвх - дизайн, установка.
            ";

        [Test]
        public void EncodeDecode()
        {
            Base64BodyInfo str = new  Base64BodyInfo();
            MemoryStream ms = new MemoryStream();
            str.Encode(new MemoryStream(Encoding.GetEncoding("koi8-r").GetBytes(testdata)), ms);
            MemoryStream outstr = new MemoryStream();
           
            ms.Position = 0;
            str.Decode(ms, outstr);
            string decoded = Encoding.GetEncoding("koi8-r").GetString(outstr.ToArray());
            Assert.AreEqual(testdata, decoded);
        }
        [Test]
        public void LeftoverBytesHAndling()
        {
            /*
            The Input: leasure.   Encodes to bGVhc3VyZS4=
            The Input: easure.    Encodes to ZWFzdXJlLg==
            The Input: asure.     Encodes to YXN1cmUu
            The Input: sure.      Encodes to c3VyZS4=


             */

            Base64BodyInfo str = new Base64BodyInfo();
            MemoryStream ms = new MemoryStream();
            str.Encode(new MemoryStream(Encoding.ASCII.GetBytes("leasure.")), ms); 
            ms.Position = 0;
            Assert.That(new StreamReader(ms).ReadToEnd(),Is.EqualTo("bGVhc3VyZS4="));

            ms = new MemoryStream();
            str.Encode(new MemoryStream(Encoding.ASCII.GetBytes("easure.")), ms);
            ms.Position = 0;
            Assert.That(new StreamReader(ms).ReadToEnd(), Is.EqualTo("ZWFzdXJlLg=="));

            ms = new MemoryStream();
            str.Encode(new MemoryStream(Encoding.ASCII.GetBytes("asure.")), ms);
            ms.Position = 0;
            Assert.That(new StreamReader(ms).ReadToEnd(), Is.EqualTo("YXN1cmUu"));

            ms = new MemoryStream();
            str.Encode(new MemoryStream(Encoding.ASCII.GetBytes("sure.")), ms);
            ms.Position = 0;
            Assert.That(new StreamReader(ms).ReadToEnd(), Is.EqualTo("c3VyZS4="));
       


        }
    }
}